<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { userLogin } from '@/api/user'
import { ElMessage } from "element-plus";

const router = useRouter()
// 表单数据
const form = reactive({
  account: '',
  password: '',
  rememberMe: false
})
// 错误信息
const errors = reactive({
  account: '',
  password: ''
})
// 加载状态
const isLoading = ref(false)
// 密码可见性
const showPassword = ref(false)

// 切换密码可见性
const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value
}

// 登录方法
const handleLogin = async () => {
  // 重置错误信息
  errors.account = ''
  errors.password = ''
  // 非空检验
  if (!form.account.trim()) {
    errors.account = '请输入账号'
    return
  }
  if (!form.password) {
    errors.password = '请输入密码'
    return
  }
  // 设置加载状态
  isLoading.value = true
  try {
    // 定义不同类型的查询
    const queries = [
      { type: 'username', value: form.account },
      { type: 'phone', value: form.account },
      { type: 'email', value: form.account },
      { type: 'wechatId', value: form.account }
    ]
    // 顺序尝试每个查询
    for (const query of queries) {
      const requestData = {
        [query.type]: query.value,
        password: form.password
      }
      const res = await userLogin(requestData)
      // 如果返回有效用户数据，立即跳转
      if (res.data && res.data.id != null) {
        if (res.data.status !== 1){
          ElMessage.error('账户异常')
          return
        }
        // 跳转到首页
        router.push('/user/recommendHomePage')
        return // 成功后退出函数，不再继续尝试
      }
    }
    // 如果所有查询都失败，显示错误
    errors.account = '账号或密码错误'
  } catch (error) {
    // 处理网络错误
    console.error('登录请求失败:', error)
    errors.account = '登录失败，请稍后再试'
  } finally {
    // 重置加载状态
    isLoading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="login-bg">
      <div class="bg-pattern"></div>
      <div class="bg-gradient"></div>
    </div>

    <div class="login-wrapper">
      <!-- 左侧品牌区 -->
      <div class="login-brand">
        <div class="brand-content">
          <div class="logo">
            <!-- 与首页一致的对称式logo：对话气泡+单星星 -->
            <svg
                class="logo-icon"
                viewBox="0 0 48 48"
                fill="none"
            >
              <!-- 外层对称气泡形状 -->
              <path d="M12 24C12 15.1634 19.1634 8 28 8C36.8366 8 44 15.1634 44 24C44 32.8366 36.8366 40 28 40C23.5817 40 19.5291 38.2426 16.5858 35.2993L12 40V24Z"
                    stroke="#FFFFFF" stroke-width="2.5" fill="rgba(255,255,255,0.1)"/>

              <!-- 中央评分星星 -->
              <path d="M24 14L26.5 22L34 22L28 26.5L30 34L24 30L18 34L20 26.5L14 22L21.5 22L24 14Z"
                    fill="#FFFFFF"/>
            </svg>
            <span class="logo-text">点评通</span>
          </div>

          <h3 class="brand-slogan">发现身边的美好</h3>
          <p class="brand-desc">连接用户与商家的桥梁，让消费更透明</p>

          <!-- 品牌特点 -->
          <div class="brand-features">
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-check-circle"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">真实点评，拒绝虚假内容</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-star"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">智能推荐，精准匹配需求</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-comments"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">互动交流，分享消费体验</div>
            </div>
            <div class="feature-item">
              <div class="feature-icon">
                <div class="icon-inner">
                  <i class="fa fa-shield"></i>
                  <span class="icon-star">★</span>
                </div>
                <div class="icon-pulse"></div>
              </div>
              <div class="feature-text">安全保障，严格保护隐私</div>
            </div>
          </div>

          <!-- 品牌承诺 -->
          <div class="brand-promise">
            <p class="promise-text">我们承诺为您提供最真实的消费体验和最优质的商家服务</p>
          </div>
        </div>
      </div>

      <!-- 右侧登录表单 -->
      <div class="login-form-container">
        <div class="login-form-wrapper">
          <div class="form-header">
            <h2 class="form-title">用户登录</h2>
            <p class="form-subtitle">欢迎回来，请登录您的账户</p>
          </div>

          <form @submit.prevent="handleLogin" class="login-form">
            <div class="form-group">
              <label for="account" class="form-label">账号</label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-user"></i>
                </span>
                <input
                    type="text"
                    id="account"
                    v-model="form.account"
                    placeholder="请输入手机号/邮箱/微信号"
                    required
                    :class="{ 'is-error': errors.account }"
                    autofocus
                >
              </div>
              <p v-if="errors.account" class="error-message">{{ errors.account }}</p>
            </div>

            <div class="form-group">
              <label for="password" class="form-label">密码</label>
              <div class="form-input-wrapper">
                <span class="input-icon">
                  <i class="fa fa-lock"></i>
                </span>
                <input
                    :type="showPassword ? 'text' : 'password'"
                    id="password"
                    v-model="form.password"
                    placeholder="请输入密码"
                    required
                    :class="{ 'is-error': errors.password }"
                >
                <button
                    type="button"
                    class="toggle-password"
                    @click="togglePasswordVisibility"
                    aria-label="切换密码可见性"
                >
                  <i :class="showPassword ? 'fa fa-eye' : 'fa fa-eye-slash'"></i>
                </button>
              </div>
              <p v-if="errors.password" class="error-message">{{ errors.password }}</p>
            </div>

            <div class="form-options">
              <div class="remember-me">
                <label class="custom-checkbox">
                  <input type="checkbox" v-model="form.rememberMe">
                  <span class="checkmark"></span>
                  <span class="label-text">记住我</span>
                </label>
              </div>
              <a href="/forgot-password" class="forgot-password">忘记密码?</a>
            </div>

            <button
                type="submit"
                class="login-button"
                :disabled="isLoading"
            >
              <span v-if="!isLoading">登录</span>
              <span v-else class="loading-state">
                <i class="fa fa-spinner fa-spin"></i> 登录中...
              </span>
            </button>

            <p class="register-text">
              还没有账号？<router-link to="/userRegisterPage" class="register-link">立即注册</router-link>
            </p>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
// 颜色变量
$primary: #2563EB;
$primary-light: #3B82F6;
$primary-dark: #1D4ED8;
$primary-glass: rgba(37, 99, 235, 0.1);
$primary-bg: #EFF6FF;
$text-primary: #1E293B;
$text-secondary: #64748B;
$text-tertiary: #94A3B8;
$bg-light: #F8FAFC;
$white: #FFFFFF;
$danger: #FF4D4F;
$shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
$shadow-hover: 0 8px 30px rgba(0, 0, 0, 0.12);
$transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);

// 基础样式
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.page-wrapper {
  background-color: $bg-light;
  min-height: 100vh;
  color: $text-primary;
}

// 登录容器
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  padding: 20px;
}

// 背景装饰
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;

  .bg-pattern {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%232563eb' fill-opacity='0.05'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  }

  .bg-gradient {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, $primary-bg 0%, $white 100%);
  }
}

// 登录包装器
.login-wrapper {
  display: flex;
  width: 90%;
  max-width: 1200px;
  min-height: 620px;
  background-color: $white;
  border-radius: 24px;
  box-shadow: $shadow-hover;
  position: relative;
  z-index: 1;
  overflow: hidden;

  @media (max-width: 992px) {
    flex-direction: column;
    min-height: auto;
    margin: 20px 0;
  }
}

// 品牌区域
.login-brand {
  flex: 1;
  background: linear-gradient(135deg, $primary 0%, $primary-dark 100%);
  padding: 60px;
  display: flex;
  align-items: center;
  color: $white;
  position: relative;

  &::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    width: 100px;
    height: 100px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 0 0 0 100px;
  }

  @media (max-width: 992px) {
    padding: 40px;
  }

  .brand-content {
    width: 100%;
    position: relative;
    z-index: 1;

    .logo {
      display: flex;
      align-items: center;
      gap: 14px;
      margin-bottom: 48px;

      .logo-icon {
        width: 52px;
        height: 52px;
        transition: transform 0.3s ease;

        &:hover {
          transform: rotate(5deg) scale(1.05);
        }
      }

      .logo-text {
        font-size: 32px;
        font-weight: 700;
        letter-spacing: -0.5px;
        font-family: 'Segoe UI', system-ui, sans-serif;
      }
    }

    .brand-slogan {
      font-size: 28px;
      font-weight: 600;
      margin-bottom: 16px;
      line-height: 1.3;
    }

    .brand-desc {
      font-size: 17px;
      line-height: 1.7;
      color: rgba(255, 255, 255, 0.85);
      margin-bottom: 60px;
      max-width: 420px;
    }

    // 品牌特点优化样式
    .brand-features {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 22px;
      margin-bottom: 60px;

      .feature-item {
        display: flex;
        align-items: center;
        gap: 14px;
        padding: 18px 22px;
        background-color: rgba(255, 255, 255, 0.05);
        border-radius: 12px;
        transition: $transition;
        white-space: nowrap;

        &:hover {
          background-color: rgba(255, 255, 255, 0.1);
          transform: translateY(-5px) scale(1.02);
          box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
        }

        .feature-icon {
          width: 48px;
          height: 48px;
          background-color: rgba(255, 255, 255, 0.1);
          border-radius: 12px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 20px;
          position: relative;
          z-index: 1;

          .icon-inner {
            position: relative;
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;

            .icon-star {
              position: absolute;
              color: rgba(255, 255, 255, 0.7);
              font-size: 12px;
              transform: rotate(15deg);
              top: 8px;
              right: 10px;
            }
          }

          .icon-pulse {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.2);
            border-radius: 12px;
            transform: scale(0);
            opacity: 0;
            z-index: -1;
            animation: pulse 2.5s infinite;
          }
        }

        .feature-text {
          font-size: 16px;
          color: rgba(255, 255, 255, 0.95);
          font-weight: 500;
          white-space: nowrap;
          overflow: hidden;
          text-overflow: ellipsis;
          max-width: 200px;
        }
      }
    }

    @media (max-width: 768px) {
      .brand-features {
        grid-template-columns: 1fr !important;

        .feature-item {
          .feature-text {
            max-width: 100%;
          }
        }
      }
    }

    @media (max-width: 576px) {
      .brand-features {
        gap: 16px;

        .feature-item {
          padding: 14px 18px;

          .feature-icon {
            width: 42px;
            height: 42px;
            font-size: 18px;
          }

          .feature-text {
            font-size: 15px;
          }
        }
      }
    }

    .brand-promise {
      margin-top: 30px;

      .promise-text {
        font-size: 17px;
        color: rgba(255, 255, 255, 0.85);
        line-height: 1.7;
        position: relative;
        padding-left: 28px;

        &::before {
          content: "“";
          font-size: 52px;
          position: absolute;
          left: -14px;
          top: -14px;
          color: rgba(255, 255, 255, 0.2);
          font-family: Georgia, serif;
        }
      }
    }
  }
}

// 表单区域
.login-form-container {
  flex: 1;
  padding: 60px;
  display: flex;
  align-items: center;
  justify-content: center;

  @media (max-width: 992px) {
    padding: 40px;
  }

  .login-form-wrapper {
    width: 100%;
    max-width: 420px;

    .form-header {
      margin-bottom: 48px;
      text-align: center;

      .form-title {
        font-size: 32px;
        font-weight: 700;
        color: $text-primary;
        margin-bottom: 10px;
        position: relative;

        &::after {
          content: '';
          display: block;
          width: 60px;
          height: 4px;
          background: $primary;
          margin: 16px auto 0;
          border-radius: 2px;
        }
      }

      .form-subtitle {
        font-size: 17px;
        color: $text-secondary;
        max-width: 320px;
        margin: 0 auto;
      }
    }
  }
}

// 表单样式
.login-form {
  .form-group {
    margin-bottom: 28px;

    .form-label {
      display: block;
      font-size: 15px;
      font-weight: 500;
      color: $text-primary;
      margin-bottom: 10px;
    }

    .form-input-wrapper {
      position: relative;

      .input-icon {
        position: absolute;
        left: 18px;
        top: 50%;
        transform: translateY(-50%);
        color: $text-tertiary;
        font-size: 20px;
        transition: $transition;
      }

      input {
        width: 100%;
        padding: 16px 16px 16px 54px;
        border: 1px solid #E2E8F0;
        border-radius: 12px;
        font-size: 17px;
        color: $text-primary;
        transition: $transition;
        background-color: $bg-light;

        &:focus {
          outline: none;
          border-color: $primary;
          box-shadow: 0 0 0 4px $primary-glass;
          background-color: $white;

          + .input-icon {
            color: $primary;
          }
        }

        &::placeholder {
          color: $text-tertiary;
        }

        &.is-error {
          border-color: $danger;

          + .input-icon {
            color: $danger;
          }
        }
      }

      .toggle-password {
        position: absolute;
        right: 16px;
        top: 50%;
        transform: translateY(-50%);
        background: none;
        border: none;
        color: $text-tertiary;
        cursor: pointer;
        transition: $transition;
        width: 36px;
        height: 36px;
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;

        &:hover {
          color: $primary;
          background-color: $primary-glass;
        }
      }
    }

    .error-message {
      margin-top: 8px;
      font-size: 14px;
      color: $danger;
      display: flex;
      align-items: center;
      padding-left: 2px;

      &::before {
        content: '!';
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 16px;
        height: 16px;
        background-color: $danger;
        color: white;
        border-radius: 50%;
        font-size: 12px;
        margin-right: 6px;
        font-weight: bold;
      }
    }
  }

  .form-options {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 36px;

    .remember-me {
      .custom-checkbox {
        display: flex;
        align-items: center;
        cursor: pointer;

        input {
          position: absolute;
          opacity: 0;
          cursor: pointer;
          height: 0;
          width: 0;
        }

        .checkmark {
          position: relative;
          height: 22px;
          width: 22px;
          border: 1px solid #E2E8F0;
          border-radius: 6px;
          transition: $transition;
          background-color: $bg-light;

          &::after {
            content: "";
            position: absolute;
            display: none;
            left: 8px;
            top: 4px;
            width: 6px;
            height: 12px;
            border: solid $white;
            border-width: 0 2px 2px 0;
            transform: rotate(45deg);
          }
        }

        input:checked ~ .checkmark {
          background-color: $primary;
          border-color: $primary;
        }

        input:checked ~ .checkmark::after {
          display: block;
        }

        .label-text {
          margin-left: 10px;
          font-size: 15px;
          color: $text-secondary;
        }
      }
    }

    .forgot-password {
      font-size: 15px;
      color: $primary;
      text-decoration: none;
      transition: $transition;
      font-weight: 500;

      &:hover {
        color: $primary-dark;
        text-decoration: underline;
      }
    }
  }

  .login-button {
    width: 100%;
    padding: 16px;
    background: linear-gradient(135deg, $primary 0%, $primary-dark 100%);
    color: $white;
    border: none;
    border-radius: 12px;
    font-size: 18px;
    font-weight: 600;
    cursor: pointer;
    transition: $transition;
    box-shadow: 0 4px 15px rgba(37, 99, 235, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;

    &:hover:not(:disabled) {
      transform: translateY(-3px);
      box-shadow: 0 8px 25px rgba(37, 99, 235, 0.35);
    }

    &:disabled {
      background: #E2E8F0;
      cursor: not-allowed;
      box-shadow: none;
    }

    .loading-state {
      display: flex;
      align-items: center;
      gap: 8px;
    }
  }

  .register-text {
    margin-top: 30px;
    text-align: center;
    font-size: 15px;
    color: $text-secondary;

    .register-link {
      color: $primary;
      font-weight: 600;
      text-decoration: none;
      transition: $transition;

      &:hover {
        color: $primary-dark;
        text-decoration: underline;
      }
    }
  }
}

// 动画
@keyframes pulse {
  0% {
    transform: scale(0);
    opacity: 0.5;
  }
  50% {
    transform: scale(1);
    opacity: 0;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

// 响应式优化
@media (max-width: 768px) {
  .brand-features {
    grid-template-columns: 1fr !important;
  }

  .login-brand {
    padding: 40px 30px;

    .brand-slogan {
      font-size: 24px;
    }
  }

  .form-header {
    margin-bottom: 36px !important;

    .form-title {
      font-size: 28px;
    }
  }
}

@media (max-width: 576px) {
  .login-wrapper {
    min-height: auto;
    border-radius: 16px;
  }

  .login-brand {
    padding: 30px 20px;

    .logo {
      margin-bottom: 30px;

      .logo-icon {
        width: 44px;
        height: 44px;
      }

      .logo-text {
        font-size: 26px;
      }
    }

    .brand-desc {
      margin-bottom: 40px;
      font-size: 16px;
    }

    .brand-features {
      gap: 16px;

      .feature-item {
        padding: 14px 18px;

        .feature-icon {
          width: 36px;
          height: 36px;
          font-size: 18px;
        }

        .feature-text {
          font-size: 15px;
        }
      }
    }
  }

  .login-form-container {
    padding: 30px 20px;
  }

  .form-group {
    margin-bottom: 22px !important;
  }

  input {
    padding: 14px 14px 14px 50px !important;
    font-size: 16px !important;
  }

  .login-button {
    padding: 14px !important;
    font-size: 17px !important;
  }
}
</style>